package com.csdj.realm;

import com.csdj.pojo.SyEmp;
import com.csdj.service.SyEmpService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.scanner.ScannerImpl;

/**
 * @author 涵哥
 * @date 2023-09-01 9:59
 */
@Component
public class MyRealm extends AuthorizingRealm {

    @Autowired
    private SyEmpService syEmpService;
    /**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    /**
     * 认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        // 拿到员工号
        String empNo = authenticationToken.getPrincipal().toString();
        SyEmp emp = syEmpService.login(empNo);
        if (emp == null) {
            throw  new UnknownAccountException("用户不存在");
        }else if (emp.getDisabled()==1){
            throw new AuthenticationException("该账号以被禁用");
        }
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(authenticationToken.getPrincipal(), emp.getPwd(), this.getName());

        return simpleAuthenticationInfo;
    }
}
